const http = require('http')
const fs = require('fs')
const url = require('url')
const mysql = require('mysql')
const io = require('socket.io')

const regExps = require('../lib/reg')

let db = mysql.createPoolCluster({ host: 'localhost', user: 'root', password: '', database: 'test', port: '3308'})

let httpServer = http.createServer((req, res) => {
    console.log(url.parse(req.url), true)
    let { pathname, query } = url.parse(req.url, true)

    if (pathname === '/reg') {
        let { username, pass } = query
        if (!regExps.username.test(username)) {
            res.write(JSON.stringify({code: '1111', message: '用户名不符合规范'}))
            res.end()
        }
        if (!regExps.password.test(pass)) {
            res.write(JSON.stringify({code: '1111', message: '密码不符合规范'}))
            res.end()
        }

        db.query(`SELECT ID FROM user_talbe WHERE username='${username}'`, (error, data) => {
            if (error) {
                res.write(JSON.stringify({ code: '1111', message: error}))
                res.end()
            } else if (data.length > 0) {
                res.write(JSON.stringify({code: '1111', message: '用户名已经存在'}))
                res.end()
            } else {
                db.query(`INSERT INTO user_table (username, password, online) VALUES ('${username}', '${pass}', 1)`, error => {
                    if (error) {
                        res.write(JSON.stringify({code: '1111', message: error}))
                        res.end()
                    } else {
                        res.write(JSON.stringify({code: '0000', message: '恭喜您，注册成功'}))
                        res.end()
                    }
                })
            }
        })

    } else if (pathname === '/login') {
        let { username, pass } = query

        if(!regExps.username.test(username)) {
            res.write(JSON.stringify({code: '1111', message: '用户名或密码错误'}))
            res.end()
        } else if (!regExps.password.test(pass)) {
            res.write(JSON.stringify({code: '1111', message: '用户名或密码错误'}))
            res.end()
        } else {
            db.query(`SELECT ID, password FROM user_table WHERE username='${username}'`, (error, data) => {
                if (error) {
                    res.write(JSON.stringify({code: '1111', message: error}))
                    res.end()
                } else if (data.length === 0) {
                    res.write(JSON.stringify({code: '1111', message: '该用户不存在'}))
                    res.end()
                } else if (data[0].password !== pass) {
                    res.write(JSON.stringify({code: '1111', message: '用户名或密码错误'}))
                    res.end()
                } else {
                    db.query(`UPDATE user_table SET online=1 WHERE ID=${data[0].ID}`, error => {
                        if (error) {
                            res.write(JSON.stringify({code: '1111', message: error}))
                            res.end()
                        } else {
                            res.write(JSON.stringify({code: '0000', message: '登陆成功'}))
                            res.end()
                        }
                    })
                }
            })
        }
    } else {
        fs.readFile(`www${pathname}`, (error, data) => {
            if (error) {
                res.setHeader(404)
                res.write('Not found', JSON.stringify({error}))
            } else {
                res.write(data)
            }
            res.end()
        })
    }
})

httpServer.listen(4010)
console.log(`http server running at http://localhost:4010`)